/**
 * 
 */
package service.impl;

import java.util.*;
import dao.impl.*;
import dao.*;
import entity.*;
import service.impl.*;
import service.GroupManagement;

/**
 * @author markT
 *
 */
public class GroupManagementImp implements GroupManagement {

	/* （非 Javadoc）
	 * @see service.GroupManagement#groupIn(java.util.List)
	 */
	@Override
	public boolean groupIn(List<Student> stuList) {
		// TODO 自动生成的方法存根
		System.out.println("安排人数：  "+stuList.size());
		StudentDaoImpl sdi = new StudentDaoImpl();
		
		Map<Dormitory,Integer> manD = new HashMap<Dormitory,Integer>();
		Map<Dormitory,Integer> womanD = new HashMap<Dormitory,Integer>();
		List<Dormitory> listDor = DormitoryDaoFactory.getDormitoryDao().findAll();
		for(int i=0;i<listDor.size();i++) {
			String sexDor = ApartmentDaoFactory.getApartmentDao().find(listDor.get(i).getId_apart()).getSex();
			if(sexDor.equals("男")) {
				manD.put(listDor.get(i), listDor.get(i).getSize());
			}else if(sexDor.equals("女")) {
				womanD.put(listDor.get(i), listDor.get(i).getSize());
			}
		}
		//遍历传入学生
		for(int i=0;i<stuList.size();i++) {
			Set<Dormitory> manSet = manD.keySet();
			Set<Dormitory> womanSet = womanD.keySet(); 
			Student s = stuList.get(i);
			if(s.getId_bed()!= 1&&s.getId_bed()!= 2&&s.getId_bed()!= 3&&s.getId_bed()!=4) {
				String sex = s.getSex();
				if(sex.equals("男")) {
					//遍历空宿舍
					Iterator<Dormitory> it1 = manSet.iterator();
			        while(it1.hasNext()){
			        	Dormitory d = it1.next();
			        	//找到宿舍内的空位
			        	int idDor = d.getId(),idAprt = d.getId_apart();
			        	FindStuImpl findstu = new FindStuImpl();
			        	List<Student> stuDor = findstu.findStuByDorm(idDor,idAprt);
			        	int [] seat = new int[4];
			        	for(int j =0;j<stuDor.size();j++) {
			        		seat[stuDor.get(j).getId_bed()] = 1;
			        	}
			        	int flag = -1;
		        		for(int k=0;k<4;k++) {
		        			if(seat[k] != 1) {
		        				flag = k;
		        				break;
		        			}
			        	}
		        		//改值
		        		int cid = s.getId(),cAprtId = d.getId_apart(),cDormId = d.getId();
		        			//将学生id 为cid 的公寓信息改为上值
		        			//修改公寓。宿舍的数据库信息
		        		s.setId_apart(cAprtId);
		        		s.setId_dorm(cDormId);
		        		s.setId_bed(flag );		//改一下!!!
		        		sdi.update(s);
		        		System.out.println("\t\t"+cid+" 搬进了 "+cAprtId+"号公寓"+cDormId+"号宿舍  "+flag+"号床");
		        		//stuList.remove(s);
			        	//更改宿舍空位
			        	int v = manD.get(d);
			        	manD.put(d,v-1);
			        	break;
			        }
				}else if(sex.equals("女")) {
					//遍历空宿舍
					Iterator<Dormitory> it1 = womanSet.iterator();
			        while(it1.hasNext()){
			        	Dormitory d = it1.next();
			        	//找到宿舍内的空位
			        	int idDor = d.getId(),idAprt = d.getId_apart();
			        	FindStuImpl findstu = new FindStuImpl();
			        	List<Student> stuDor = findstu.findStuByDorm(idDor,idAprt);
			        	int [] seat = new int[4];
			        	for(int j =0;j<stuDor.size();j++) {
			        		seat[stuDor.get(j).getId_bed()] = 1;
			        	}
			        	int flag = -1;
		        		for(int k=0;k<4;k++) {
		        			if(seat[k] != 1) {
		        				flag = k;
		        				break;
		        			}
			        	}
		        		//改值
		        		int cid = s.getId(),cAprtId = d.getId_apart(),cDormId = d.getId();
		        			//将学生id 为cid 的公寓信息改为上值
		        			//修改公寓。宿舍的数据库信息
		        		s.setId_apart(cAprtId);
		        		s.setId_dorm(cDormId);
		        		s.setId_bed(flag );	//改一下!!!
		        		sdi.update(s);
		        		System.out.println("\t\t"+cid+" 搬进了 "+cAprtId+"号公寓"+cDormId+"号宿舍  "+flag+"号床");
		        		//stuList.remove(s);
			        	//更改宿舍空位
			        	int v = womanD.get(d);
			        	womanD.put(d,v-1);
			        	break;
			        }
				}
			}else {
				System.out.println("有人被安排了");
				return false;
			}
		}
		return true;
	}

	/* （非 Javadoc）
	 * @see service.GroupManagement#gruopOut(java.util.List)
	 */
	@Override
	public boolean gruopOut(List<Student> stuList) {
		// TODO 自动生成的方法存根
		StudentDaoImpl sdi = new StudentDaoImpl();
		System.out.println("安排人数：  "+stuList.size());
		
		Iterator<Student> it1 = stuList.iterator();
		while(it1.hasNext()) {
			Student d = it1.next();
			d.setId_apart(-1);
			d.setId_bed(-1);
			d.setId_dorm(-1);
			d.setIsHere(-1);
			System.out.println(d.getId()+" 已安排");
			sdi.update(d);
			
		}
		return false;
	}

	public void chaqinPublic() {
		List<Student> stulist = new ArrayList<Student>();
		StudentDao sd = StudentDaoFactory.getStudentDao();
		stulist = sd.findAll();
		System.out.println("夜不归寝名单：");
		for(Student stu :stulist) {
			if(stu.getIsHere() == 2)
				System.out.println("学号："+stu.getId()+" 姓名："+stu.getName()+"  公寓号："+stu.getId_apart()+"   宿舍号："+stu.getId_dorm()+"  床号："+stu.getId_bed());
		}
	}
	public void stat() {
		List<Student> stulist = new ArrayList<Student>();
		StudentDao sd = StudentDaoFactory.getStudentDao();
		stulist = sd.findAll();
		System.out.println("学生在寝情况统计：");
		for(Student stu :stulist) {
			if(stu.getIsHere() == 2)
				System.out.println("学号："+stu.getId()+" 姓名："+stu.getName()
				+"  公寓号："+stu.getId_apart()+"   宿舍号："+stu.getId_dorm()+"  床号："+stu.getId_bed()+"  \t\t状态： 夜不归寝");

			if(stu.getIsHere() == 1)
				System.out.println("学号："+stu.getId()+" 姓名："+stu.getName()
				+"  公寓号："+stu.getId_apart()+"   宿舍号："+stu.getId_dorm()+"  床号："+stu.getId_bed()+"  \t\t状态： 请假外出");

			if(stu.getIsHere() == 0)
				System.out.println("学号："+stu.getId()+" 姓名："+stu.getName()
				+"  公寓号："+stu.getId_apart()+"   宿舍号："+stu.getId_dorm()+"  床号："+stu.getId_bed()+"  \t\t状态： 正常就住");
		}
	}
	public boolean setChaQin() {
		StudentDao sd = StudentDaoFactory.getStudentDao();
		int i=-1;
		System.out.println("请输入学号：");
		Scanner in = new Scanner(System.in);
		i = in.nextInt();
		Student stu = sd.find(i);
		if(stu == null) {
			System.out.println("学号输入错误，不存在");
			return false;
		}else {
			stu.setIsHere(2);
			sd.update(stu);
			System.out.println("学号为"+stu.getId()+"的夜不归寝行为已记录！");
		}
		return true;
	}
}
